<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c"%>
<%
	String path = request.getContextPath();
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>设备限额管理</title>
<jsp:include page="../../import.jsp" flush="true" />

</head>
<body>
	<form id="friBaseInfo_form">
		<br>
		<table class="sq-formTable2" id="ligergrid2"  >
			<tbody>
				<tr>
					<td style="padding: 0;text-align: center; width:1010px">
						<table id="tableBank2" class="sq-formTable no-border">
							<thead>
								<tr>
								    <th class="tbLabel" style="text-align: center;background:#e7fef4;"></th>
									<th class="tbLabel" style="text-align: center;width:20%;background:#e7fef4;" >项目类型</th>
									<th class="tbLabel" style="text-align: center;width:20%;background:#e7fef4;">融资额上限（万）</th>
									<th class="tbLabel" style="text-align: center;width:18%;background:#e7fef4;">设备数上限（台）</th>
									<th class="tbLabel" style="text-align: center;width:15%;background:#e7fef4;">主车数量上限（台）</th>
									<th class="tbLabel" style="text-align: center;width:15%;background:#e7fef4;">挂车数量上限（台）</th>
									<th class="tbLabel" style="text-align: center;width:15%;background:#e7fef4;">是否支持纯挂</th>
									<th class="tbLabel" style="text-align: center;width:10%;background:#e7fef4;border-right: 1px solid #e9e9e9;">
										<span>操作 </span>
										<a href="javascript:void(0)" onclick="addRowPrjTyp(++b)">
											<font style="color:#6495ED;">（添加）</font>
										</a>
									</th>
								</tr>
							</thead>
							<tbody>

							</tbody>
						</table>
					</td>
				</tr>
			</tbody>
		</table>
		<!-- hide data -->
		<div class="fieldBox col_100" style="display:none;">
			<div class="field">
				<input name="dataId" id="dataId" type="text" class="ui-textbox" value="${dataId}">
			</div>
		</div>
		<div class="btnsBox">
			<span id="cr_bsc_info_add_btn_id" class="sq-button sq-button-round" onclick="save();">保存 </span>
		</div>
	</form>
	<script type="text/javascript">
	
		var a=0;
		var b=0;
		$(function() {
		   
		   $("#friBaseInfo_form").ligerForm({
                validate: true
           });
	       initLimData();
		});
		
		//保存
		function save(){
			var ligerForm = liger.get("friBaseInfo_form");
            if (!ligerForm.valid()) {
                UIUtils.alertWarning("有验证信息未通过!");
                return false; //验证不通过，不执行以下代码
            }
            var data = ligerForm.getData();
            var dataId = $("#dataId").val();
            var url = '<%=path%>/rfbase/fundRasInsLim/batchUpdateLim.do';
            data["dataId"]= dataId;
			//机构限额
			var typeArray = [];
			var tyes=[];
			var bankTr2 = $('#tableBank2 tbody tr');
			if(bankTr2.length<1){
				$.ligerDialog.alert("机构限额必须有一条数据");
				return ;
			}
				
			for (var i = 0; i < bankTr2.length; i++) {
				var id = $.trim($(bankTr2[i]).find("input[name='id']").val());
				var prjTypCd = $.trim($(bankTr2[i]).find("select[name='prjTypCd'] option:selected").val());
				var insLimAmt = $.trim($(bankTr2[i]).find("input[name='insLimAmt']").val());
				var insLimNum = $.trim($(bankTr2[i]).find("input[name='insLimNum']").val());
				var mainLimNum = $.trim($(bankTr2[i]).find("input[name='mainLimNum']").val());//主车数量上限	REQ-12550拆分资管机构二期
				var handLimNum = $.trim($(bankTr2[i]).find("input[name='handLimNum']").val());//挂车数量上限
				var isHang = $.trim($(bankTr2[i]).find("select[name='isHang'] option:selected").val());//是否支持纯挂
				insLimAmt = insLimAmt.replace(/\s*/g,"");//去掉所有空格
				insLimNum = insLimNum.replace(/\s*/g,"");//去掉所有空格
				mainLimNum = mainLimNum.replace(/\s*/g,"");
				handLimNum = handLimNum.replace(/\s*/g,"");
				if(1){
					if( insLimAmt == '' || insLimAmt == null || isNaN(insLimAmt) ){
						$.ligerDialog.alert("融资额上限必须是数字");
						return ;
					}
					var y = String(insLimAmt).indexOf(".") + 1;//获取小数点的位置
				    var count = String(insLimAmt).length - y;//获取小数点后的个数
				    if(y > 0 && count > 2) {
				    	$.ligerDialog.alert("融资额上限最多两位小数");
				    	return;
				    }
				    if(insLimAmt<0){
						   $.ligerDialog.alert("融资额上限必须>=0");
							return ;
					}
				}
				if(1){
					if(insLimNum == '' || insLimNum == null || isNaN(insLimAmt) ){
						$.ligerDialog.alert("设备数上限必须是数字");
						return ;
					}
					var y = String(insLimNum).indexOf(".") + 1;//获取小数点的位置
				    var count = String(insLimNum).length - y;//获取小数点后的个数
				    
				    if(y > 0) {
				    	$.ligerDialog.alert("设备数不能有小数");
				    	return ;
				    }
				   if(insLimNum<0){
					   $.ligerDialog.alert("设备数上限必须>=0");
						return ;
				   }
				}
				if(1){//mainLimNum
					if(mainLimNum == '' || mainLimNum == null || isNaN(mainLimNum) ){
						$.ligerDialog.alert("主车数量上限必须是数字");
						return ;
					}
					var y = String(mainLimNum).indexOf(".") + 1;//获取小数点的位置
				    var count = String(mainLimNum).length - y;//获取小数点后的个数
				    
				    if(y > 0) {
				    	$.ligerDialog.alert("主车数量不能有小数");
				    	return ;
				    }
				   if(mainLimNum<0){
					   $.ligerDialog.alert("主车数量上限必须>=0");
						return ;
				   }
				}
				if(1){//handLimNum
					if(handLimNum == '' || handLimNum == null || isNaN(handLimNum) ){
						$.ligerDialog.alert("挂车数量上限必须是数字");
						return ;
					}
					var y = String(handLimNum).indexOf(".") + 1;//获取小数点的位置
				    var count = String(handLimNum).length - y;//获取小数点后的个数
				    
				    if(y > 0) {
				    	$.ligerDialog.alert("挂车数量不能有小数");
				    	return ;
				    }
				   if(handLimNum<0){
					   $.ligerDialog.alert("挂车数量上限必须>=0");
						return ;
				   }
				}
				for(var j = 0; j < tyes.length; j++){
					if(tyes[j]==prjTypCd){
						$.ligerDialog.alert("项目类型不能相同");
						return;
					}	
				}
				tyes.push(prjTypCd);
				var bank = {
					"id" : id,
					"prjTypCd" : prjTypCd,
					"insLimAmt" : insLimAmt,
					"insLimNum" : insLimNum,
					"mainLimNum":mainLimNum,
					"handLimNum":handLimNum,
					"isHang":isHang
				}
				typeArray.push(bank);
			}
			data["typeArray"] = JSON.stringify(typeArray);
			
			AjaxUtils.ajaxPost(url, data, addCallback);
		}

		//新增回调
		function addCallback(msg) {
			if (msg.success == 1) {
				UIUtils.alertSuccess(msg.info);
				/* 父级的弹出框关闭 */
				frameElement.dialog.close();
				
			} else {
				UIUtils.alertError(msg.info);
			}
		}
		

		//初始化回调
		function initCallback(msg) {
			if (msg.success == 1) {
				setData(msg.data);//设置数据
			} else {
				UIUtils.alertError(msg.info);
			}
		}
		
		function delRow(obj){
			if($("#tableBank").find("tr").length > 2){
				$(obj).parent().parent().remove();
			}
			else{
				$.ligerDialog.alert('不能删除，至少保留一条数据！');
			}
		}
		
		function  setAccTyp(id){
			 $("#accTypCdNm-"+id).ligerComboBox({ isShowCheckBox: true, isMultiSelect: true,
	     	   	 data: [ { name: 'key', width: 100}, 
	     	   	         { name: 'value', width: 120}
	     	   	 ],
		          url:'<%=path%>/rfbase/fribaseinfo/getAccTypCd.do',
		          ajaxType :'GET',
		          slide: false,
		          valueField: 'key', textField: 'value',
		          valueFieldID: 'accTypCd-'+id
		     }); 
		}
		function initLimData(){
			var limArrayJSON= '${limArrayJSON}';//设备限额信息
			var limArr=eval(limArrayJSON);
			console.log("limArr"+limArr);
			addRowPrjTyp(b);
			if(limArr!=null && limArr.length>0){
				for (var i = 0; i < limArr.length; i++) {
					var bankTr = $('#tableBank2 tbody tr:eq('+i+')');
					console.log("limArr"+limArr[i].insLimNum);
					bankTr.find("input[name='id']").val(limArr[i].id);
					bankTr.find("select[name='prjTypCd']").val(limArr[i].prjTypCd);
					bankTr.find("input[name='insLimAmt']").val(limArr[i].insLimAmt);
					var tempInsLimAmt=limArr[i].insLimAmt;
					if(tempInsLimAmt ==null ||tempInsLimAmt==0){
						bankTr.find("input[name='insLimAmt']").attr("disabled",true);
						bankTr.find("input[name='checkbox1']").attr("checked",true);
						bankTr.find("input[name='insLimAmt']").val(0);
					}
					bankTr.find("input[name='insLimNum']").val(limArr[i].insLimNum);
					var tempInsLimNum = limArr[i].insLimNum;
					if(tempInsLimNum == null || tempInsLimNum==0){
						bankTr.find("input[name='checkbox2']").attr("checked",true);
						bankTr.find("input[name='insLimNum']").attr("disabled",true);
						bankTr.find("input[name='insLimNum']").val(0);
					}
					bankTr.find("input[name='mainLimNum']").val(limArr[i].mainLimNum);
					var tempMainLimNum=limArr[i].mainLimNum;
					if(tempMainLimNum == null || tempMainLimNum ==0){
						bankTr.find("input[name='checkbox3']").attr("checked",true);
						bankTr.find("input[name='mainLimNum']").attr("disabled",true);
						bankTr.find("input[name='mainLimNum']").val(0);
					}
					bankTr.find("input[name='handLimNum']").val(limArr[i].handLimNum);
					var tempHandLimNum = limArr[i].handLimNum;
					if(tempHandLimNum ==null || tempHandLimNum == 0){
						bankTr.find("input[name='checkbox4']").attr("checked",true);
						bankTr.find("input[name='handLimNum']").attr("disabled",true);
						bankTr.find("input[name='handLimNum']").val(0);
					}
					var tempIsHang = limArr[i].isHang;
					if(tempIsHang!=null && tempIsHang!='undefined' && tempIsHang!=''){
						bankTr.find("select[name='isHang']").val(tempIsHang);
					}else{//未维护 此字段 默认不支持
						bankTr.find("select[name='isHang']").val('0');
					}
					if(i < limArr.length-1){
						addRowPrjTyp(++b);
					}
				}
			}
		}
		function addRowPrjTyp(b){
			a++;
			$('#tableBank2 tbody').append('<tr>'+
				'<td style="text-align: center;">'+
					'<div class="field">'+
						a+
					'</div>'+
				'</td>'+
				'<td style="text-align: center;">'+
					
					'<div class="field">'+
						'<select name="prjTypCd" style="width:150px;" data-value=" ">'+
							'<c:forEach items="${prjTypCds }" var="kv">'+
								'<option value="${kv.key }">${kv.value }</option>'+
							'</c:forEach>'+
						'</select>'+
					'</div>'+
				'</td>'+
					'<input name="id" type="hidden" class="ui-textbox" value="" >'+
				'</td>'+
				
				'<td style="text-align: center;">'+
					'<div class="field">'+
						'<input name="insLimAmt" type="text" class="ui-textbox" value="" size=10>'+
						'<input name="checkbox1" type="checkbox" class="ui-textbox" value="" onclick="checknum1(this)">无'+
					'</div>'+
				'</td>'+
				'<td style="text-align: center;">'+
					'<div class="field">'+
						'<input name="insLimNum" type="text" class="ui-textbox" value="" size=10>'+
						'<input name="checkbox2" type="checkbox" class="ui-textbox" value="" onclick="checknum2(this)">无'+
					'</div>'+
				'</td>'+
				'<td style="text-align: center;">'+
					'<div class="field">'+
						'<input name="mainLimNum" type="text" class="ui-textbox" value="" size=10>'+
						'<input name="checkbox3" type="checkbox" class="ui-textbox" value="" onclick="checknum3(this)">无'+
					'</div>'+
				'</td>'+
				'<td style="text-align: center;">'+
					'<div class="field">'+
						'<input name="handLimNum" type="text" class="ui-textbox" value="" size=10>'+
						'<input name="checkbox4" type="checkbox" class="ui-textbox" value="" onclick="checknum4(this)">无'+
					'</div>'+
				'</td>'+
				'<td style="text-align: center;">'+
					'<div class="field">'+
						'<select name="isHang"  data-value=" ">'+
								'<option value="1">支持</option>'+
								'<option value="0">不支持</option>'+
						'</select>'+
					'</div>'+
				'</td>'+
				'<td style="text-align: center;border-right: 1px solid #e9e9e9;">'+
					'<a href="javascript:void(0)" onclick="delRow2(this)">'+
						'<font style="color:#6495ED;">删除</font>'+
					'</a>'+
				'</td>'+
			'</tr>');
			 setAccTyp(b);
			 ++b;
		}
		function delRow2(obj){
				$(obj).parent().parent().remove();
		}
		function checknum1(obj){		
			if(obj.checked==true){
				$(obj).parent().find("input[name='insLimAmt']").attr("disabled","disabled");
				$(obj).parent().find("input[name='insLimAmt']").val(0);
			}else{
				$(obj).parent().find("input[name='insLimAmt']").removeAttr("disabled");
			}
		}
		function checknum2(obj){			
			if(obj.checked==true){
				$(obj).parent().find("input[name='insLimNum']").attr("disabled","disabled");
				$(obj).parent().find("input[name='insLimNum']").val(0);
			}else{
				$(obj).parent().find("input[name='insLimNum']").removeAttr("disabled");
			}
		}
		function checknum3(obj){			
			if(obj.checked==true){
				$(obj).parent().find("input[name='mainLimNum']").attr("disabled","disabled");
				$(obj).parent().find("input[name='mainLimNum']").val(0);
			}else{
				$(obj).parent().find("input[name='mainLimNum']").removeAttr("disabled");
			}
		}
		function checknum4(obj){			
			if(obj.checked==true){
				$(obj).parent().find("input[name='handLimNum']").attr("disabled","disabled");
				$(obj).parent().find("input[name='handLimNum']").val(0);
			}else{
				$(obj).parent().find("input[name='handLimNum']").removeAttr("disabled");
			}
		}
	</script>
	<script type="text/javascript">
	$(document).ready(function() {
		//英文
		$.validator.addMethod("English", function(value, element) {
	        return this.optional(element) || /^[a-zA-Z]+$/.test(value);
	    }, "该字段只能为英文");
	});
	</script>
</body>
</html>
